package com.aboo.users.action;

import com.aboo.frame.constant.FlagEnum;
import com.aboo.frame.vo.ReturnVO;
import com.aboo.users.domain.UserPO;
import com.aboo.users.facade.UserFacade;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserAction {

    @Resource
    private UserFacade userFacade;

    @RequestMapping("/saveUser")
    public ReturnVO<UserPO> saveUser() {
        ReturnVO<UserPO> rv = new ReturnVO<>();

        LocalDateTime ldt = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String dateStr = dtf.format(ldt);

        UserPO user = new UserPO();
        user.setUserCode("User" + dateStr);
        user.setUserName("测试版");
        user.setEctFlag(FlagEnum.SUCCESS.getValue());
        userFacade.saveUser(user);
        rv.setData(user);
        
        log.info(JSONObject.toJSONString(user));
        return rv;
    }

    @RequestMapping("/getUsers")
    public ReturnVO<Object> getUsers() {
        ReturnVO<Object> rv = new ReturnVO<>();
        List<UserPO> list = userFacade.getUser("1", "5");
        rv.setData(list);

        return rv;
    }

}
